package com.lgx.mall.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.core.dao.IProductDao;
import com.lgx.mall.core.entity.Product;
import com.lgx.mall.core.service.IProductService;

@Service("productService")
public class ProductService implements IProductService {

	@Autowired
	private IProductDao productDao;
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	public IProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(IProductDao productDao) {
		this.productDao = productDao;
	}

	public Product createProduct(Product product) throws DataException {
		return productDao.createProduct(product);
	}

	public Product updateProduct(Product product) throws DataException {
		return productDao.updateProduct(product);
	}

	public void deleteProduct(Product product) throws DataException {
		productDao.deleteProduct(product);
	}

	public void deleteProduct(long id) throws DataException {
		productDao.deleteProduct(id);
	}

	public Product findProductById(long id) {
		return productDao.findProductById(id);
	}

	public List<Product> findProductList(String sql) {
		return productDao.findProductList(sql);
	}
	
	public List<Product> findProductList() {
		return productDao.findProductList();
	}
	
	public Pagination findProductPagination(Object[] params, int pageSize, int pageNum){
		return this.productDao.findProductPagination(params, pageSize, pageNum);
	}
	
	public void deleteProducts(String[] ids) {
		
		try {
			this.productDao.deleteProducts(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Product> findProductByCidAndPrice(long categoryId, String type) {
		return this.productDao.findProductByCidAndPrice(categoryId, type);
	}
	public List<Product> findProductByCidAndPrice2(long categoryId, String type) {
		return this.productDao.findProductByCidAndPrice2(categoryId, type);
	}
	
	public Pagination findProductByCategoryAndPrice(long categoryId,
			String type, int pageSize, int pageNum) {
		return this.productDao.findProductByCategoryAndPrice(categoryId, type, pageSize, pageNum);
	}
	
	public Pagination findProductByCategoryAndPrice2(String areaNode,long categoryId,
			String type,String sortNumber, int pageSize, int pageNum) {
		return this.productDao.findProductByCategoryAndPrice2(areaNode,categoryId, type, sortNumber,pageSize, pageNum);
	}
	public List<Product> findProductByIdAndBrand(long id,long brandId) {
		return this.productDao.findProductByIdAndBrand(id, brandId);
	}

	public List<Product> guessYouLike(int pageSize) {
		return this.productDao.guessYouLike(pageSize);
	}

	

	public List<Product> findProductByCategory(String areaNode,
			long areaCategory, long categoryId, long brandId) {
		return this.productDao.findProductByCategory(areaNode, areaCategory, categoryId, brandId);
	}

	public Pagination findProductByCategory(String areaNode, long areaCategory,
			long categoryId, long brandId,String time, String price, int pageSize, int pageNum) {
		return this.productDao.findProductByCategory(areaNode, areaCategory, categoryId, brandId,time, price, pageSize, pageNum);
	}


	public List<Product> findProductByNameOrCode(String params) {
		return this.productDao.findProductByNameOrCode(params);
	}

	public Pagination findProductByNameOrCode(String params, String time,
			String price, int pageSize, int pageNum) {
		return this.productDao.findProductByNameOrCode(params, time, price, pageSize, pageNum);
	}

	public Pagination findProductByBrandName(String params, String time,
			String price, int pageSize, int pageNum) {
		return this.productDao.findProductByBrandName(params, time,price, pageSize, pageNum);
	}


	public void modifyProductInventory(long productId, int amount, String sign) {
		Product p = this.findProductById(productId);
		
		if("+".equals(sign)){
			p.setLeaves(p.getLeaves()+amount);
		}else if("-".equals(sign)){
			p.setLeaves(p.getLeaves()-amount);
		}
		
		try {
			this.updateProduct(p);
		} catch (DataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}
